/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xuexiang.xui_lib.component.dialog.providers;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.xuexiang.xui_lib.ResourceTable;
import com.xuexiang.xui_lib.component.dialog.building.DialogBuilder;
import com.xuexiang.xui_lib.component.dialog.config.ChooseBean;

import java.util.List;

/**
 * 适配器
 *
 * @since 2021-04-08
 */
public class SingleChooseProviders extends RecycleItemProvider {
    private List<ChooseBean> list;
    private Context context;
    private DialogBuilder dialogBuilder;
    public SingleChooseProviders(Context context,DialogBuilder dialogBuilder, List<ChooseBean> list) {
        this.context = context;
        this.dialogBuilder = dialogBuilder;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        final Text item;
        if (dialogBuilder.isListDialog()) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ios_single_choose_item, null, false);
            item = (Text) cpt.findComponentById(ResourceTable.Id_ios_single_choose_list_item_tv);
            cpt.setTag(item);
        } else {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ios_my_single_choose_item, null, false);
            item = (Text) cpt.findComponentById(ResourceTable.Id_ios_single_choose_list_item_tv);
            cpt.setTag(item);
        }

        ChooseBean chooseBean = list.get(i);
        item.setText(chooseBean.getTxt().toString());
        return cpt;
    }
}
